package cn.com.gridinfo.sooc.core.expand.callname.fragment;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jeremy.arad.utils.Log;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocFragment;
import cn.com.gridinfo.sooc.core.expand.callname.common.CodeConst;
import cn.com.gridinfo.sooc.core.expand.callname.common.PageConst;
import cn.com.gridinfo.sooc.core.expand.callname.dao.CallNameDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 实到 Fragment
 * Created by yingchunli on 2015/12/8.
 */
public class ShiDaoFragment extends BaseSoocFragment {

    String kcid;//课程id
    String ktid;//课堂id
    View view;//当前界面
    //    @InjectView(R.id.count_ren_signinfo_shidao)
    public static TextView countRen;//人数
    public static RelativeLayout photoContainer;//拍照签到区域
    @InjectView(R.id.looksignphoto)
    TextView looksignphoto;//查看签到照片
    @InjectView(R.id.qdzp)
    ImageView qdIcon;//签到照片icon   签到列表icon
    private FragmentManager fm;
    CallNameDao dao;
    String pzqd;//拍照签到

    public static ShiDaoFragment newInstance(String info) {
        Bundle args = new Bundle();
        ShiDaoFragment fragment = new ShiDaoFragment();
        args.putString("info", info);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.expand_signinfo_shidao, null);
        ButterKnife.inject(this, view);
        dao = new CallNameDao(this);
        countRen = (TextView) view.findViewById(R.id.count_ren_signinfo_shidao);
        photoContainer = (RelativeLayout)view.findViewById(R.id.photo_Container);
        String k[] = getArguments().getString("info").split(",");
        ktid = k[0];
        kcid = k[1];
        pzqd = k[2];
        Log.d("实到-》ktid" + ktid);
        Log.d("实到-》kcid" + kcid);
        looksignphoto.setVisibility(View.VISIBLE);
        fm = getActivity().getSupportFragmentManager();
        initView();
        Log.d("pzqd=" + pzqd);
        if (pzqd.equals("1")) {
            //需要拍照签到
            //显示切换图标和文字
            qdIcon.setVisibility(View.VISIBLE);
            looksignphoto.setVisibility(View.VISIBLE);
        } else {
            //隐藏切换图标和文字
            qdIcon.setVisibility(View.GONE);
            looksignphoto.setVisibility(View.GONE);
        }
        PageConst.pageNo = 1;
        dao.LookClassInfo(ktid, PageConst.pageNo, PageConst.pageSize, CodeConst.EXPAND_SIGNINFO_SHIDAO_FRIST);
        return view;
    }

    /***
     * 初始化界面
     */
    private void initView() {
        qdIcon.setImageResource(R.mipmap.expand_callname_qdzp);
        looksignphoto.setText("签到照片");
        ShiDaoListFragment shiDaoList = new ShiDaoListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("ktid", ktid);
        bundle.putString("kcid", kcid);
        shiDaoList.setArguments(bundle);
        fm.beginTransaction().replace(R.id.fragment_content, shiDaoList, ktid).commit();
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        //判断Fragment中的ListView时候存在，判断该Fragment时候已经正在前台显示  通过这两个判断，就可以知道什么时候去加载数据了
        if (isVisibleToUser && isVisible() ) {
            initView();
        }
        super.setUserVisibleHint(isVisibleToUser);
    }

    /**
     * 查看签到照片的点击
     */
    @OnClick(R.id.looksignphoto)
    void changeFragment() {
        if (looksignphoto.getText().equals("签到照片")) {
            //切换到签到 照片 的Fragment
            qdIcon.setImageResource(R.mipmap.expand_callname_qdlb);
            looksignphoto.setText("签到列表");
            ShiDaoSignPhotoFragment shiDaoSignPhoto = new ShiDaoSignPhotoFragment();
            Bundle bundle = new Bundle();
            bundle.putString("kcid", kcid);
            bundle.putString("ktid", ktid);
            shiDaoSignPhoto.setArguments(bundle);
            fm.beginTransaction().replace(R.id.fragment_content, shiDaoSignPhoto).commit();
        } else {
            //切换到签到 列表 的Fragment
            qdIcon.setImageResource(R.mipmap.expand_callname_qdzp);
            looksignphoto.setText("签到照片");
            ShiDaoListFragment shiDaoList = new ShiDaoListFragment();
            Bundle bundle = new Bundle();
            bundle.putString("ktid", ktid);
            bundle.putString("kcid", kcid);
            shiDaoList.setArguments(bundle);
            fm.beginTransaction().replace(R.id.fragment_content, shiDaoList).commit();
        }
    }

    @Override
    public void onRequestSuccess(int requestCode) {
        super.onRequestSuccess(requestCode);
        try {
            if (dao.getStatus().equals("0")) {
                switch (requestCode) {
                    case CodeConst.EXPAND_SIGNINFO_SHIDAO_FRIST:
                        //设置人数
                        countRen.setText(dao.getClassInfo().getQdrs());

                        break;
                }
            } else {
                MessageUtils.showImageShortToast(getActivity(), dao.getStatusCode());
            }
        } catch (Exception e) {
            MessageUtils.showImageShortToast(getActivity(), "请求数据异常");
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
